<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 11:14
 */

namespace app\admin\controller;
use app\admin\model\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\helper;

class  Goods extends Common
{
    public $obj="";
    public $table="goods";
    public $date="";
    public $go="goods";
    public function __initialize(){
       parent::__initialize();

    }
    public function show(){
        $list = Db::table('goods')->where("deleted=2")->order("id","desc")->paginate(10);
        //分页
        $page=$list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        //分页输出显示
        $this->assign('page',$page);
        // 渲染模板输出
        return $this->fetch($this->go.'/show');
        /*搜索功能*/


    }


public function search(){
    /*搜索功能*/
    $re=input('goods_price');
    $rs=input('good_price');
    var_dump($re);
    var_dump($rs);
    //var_dump("&$re&");die;
     if($re){
         //显示数据
        /* $list = Db::query("select * from goods WHERE  deleted=2 AND goods_price like '%$re%' order by id DESC ");*/
         $list = Db::table('goods')->where("deleted=2")->wherelike("goods_price","['$re']"-"['$rs']")->order("id","desc")->paginate(6);
         //分页
         $page=$list->render();
         // 模板变量赋值
         $this->assign('list', $list);
         //分页输出显示
         $this->assign('page',$page);
         // 渲染模板输出
         return $this->fetch($this->go.'/show');
     }else{
         $list = Db::table('goods')->where("deleted=2")->order("id","desc")->paginate(10);
         //分页
         $page=$list->render();
         // 模板变量赋值
         $this->assign('list', $list);
         //分页输出显示
         $this->assign('page',$page);
         // 渲染模板输出
         return $this->fetch($this->go.'/show');
     }
}








//添加数据页面
    public function add(){
        $list = Db::table('ategory')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
    //添加数据信息
    public function addm(){
        $object=new Base();
        $table=$this->table;
        $date=Request::instance()->post();
        //var_dump($date);die;
        //$img=$this->uploads();
        //var_dump($img);die;
        $obj=$object->add($table,$date);
        if($obj){
            $this->success('添加成功', url('Goods/show'));
        }else {
            $this->error('添加失败');
        }
    }






//信息删除
    public function dels(){
        $id=isset($_POST['id'])?$_POST['id']:"";
        $data = ['deleted' => 1];
        $del=new Base();
        $del=$del->status('goods',$id,$data);
        return json_encode($id);
    }











//修改内容页面
public function update(){
    $id=input('id');

    $info=Db::table('goods')->where("id=$id")->find();
    $this->assign('info', $info);
    var_dump($info);
    $list = Db::table('ategory')->select();
    $this->assign('list', $list);
    return $this->fetch();
    }
    //修改内容
    public function updatem(){
        $object=new Base();
        $table=$this->table;
        $id=input('id');
        $date=Request::instance()->post();
        $date=array(
            "id"=>$date['id'],
            "goods_name"=>$date['goods_name'],
            "goods_price"=>$date['goods_price'],
            "goods_details"=>$date['goods_details'],
            "goods_number"=>$date['goods_number'],
            "goods_class_id"=>$date['goods_class_id'],
            "goods_appraise"=>$date['goods_appraise'],
            "goods_image_id"=>$date['goods_image_id'],
            "promotion_money"=>$date['promotion_money'],
            "promotion_money_vip"=>$date['promotion_money_vip'],
            "deleted"=>$date['deleted'],
            "sort"=>$date['sort'],
        );
        $obj=$object->upd($table,$id,$date);
        if($obj){
            $this->success('修改成功', url('Goods/show'));
        }else {
            $this->error('修改失败');
        }
    }










//单张图片上传
    public function upload(){
        header('content-type:text/html charset:utf-8');
        $dir_base = ROOT_PATH."public/admin/images";
        //	//文件上传根目录
        $file = request()->file('upload_file');
        $info = $file->move($dir_base);
        //dump($info);
        $base  = request()->root();
        $root  = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
        if ('' != $root) {
            $root = '/' . ltrim($root, '/');
        }
        if ( $info ) {
            $url=$root.DS.'admin'.DS.'images'.DS.date('Ymd').DS.$info->getFilename();
            $insertId= Db::table("goods")->field("id")->order("id desc")->select();
            $endID=$insertId['0']['id']+1;
            $date=['goods_address'=>$url,'goods_id'=>$endID];
            Db::table("goods_image")->insert($date);
            return $root.DS.'admin'.DS.'images'.DS.date('Ymd').DS.$info->getFilename();
        } else {
            return $info->getError();
        }
    }

}
